<?php

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 * @property CI_DB_active_record $db Description
 */
class Sim_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    public function getAllSim($number, $offset, $keyword, $status, $order, $group, $network, $agent)
    {
        $return = array();
        $this->db->select('A.phone, A.price, A.price_update, A.delete_flag, A.status, A.id, B.name as "network_name", C.name as "city_name", D.name as "group_name"')
            ->from('item_sim A')
            ->join('list_network B', 'A.list_network_id = B.id', 'left')
            ->join('list_city C', 'A.list_city_id = C.id', 'left')
            ->join('list_group D', 'A.list_group_id = D.id', 'left');
        if($keyword != '') {
           $this->db->like('A.phone', $keyword)
                ->or_like('B.name', $keyword)
                ->or_like('C.name', $keyword)
                ->or_like('D.name', $keyword);
        }
        if($status != '') {
            $this->db->where('A.delete_flag', $status);
        }
        if($order != '') {
            $this->db->where('A.status', $order);
        }
        if($group != '') {
            $this->db->where('A.list_group_id', $group);
        }
        if($network != '') {
            $this->db->where('A.list_network_id', $network);
        }
        if($agent != '') {
            $this->db->where('A.list_city_id', $agent);
        }
        if(is_int($offset))
            $this->db->limit($number, $offset);
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }
    
    public function getInfo($id)
    {
        $return = array();
        $this->db->select('A.*, B.name as "network_name", C.name as "city_name", D.name as "group_name"')
                ->from('item_sim A')
                ->join('list_network B', 'A.list_network_id = B.id', 'left')
                ->join('list_city C', 'A.list_city_id = C.id', 'left')
                ->join('list_group D', 'A.list_group_id = D.id', 'left')
                ->where('A.id', $id);
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }
    
    function count_all($keyword, $status, $order)
    {
        $this->db->select('A.phone')
            ->from('item_sim A')
            ->join('list_network B', 'A.list_network_id = B.id', 'left')
            ->join('list_city C', 'A.list_city_id = C.id', 'left')
            ->join('list_group D', 'A.list_group_id = D.id', 'left');
        if($keyword != '') {
           $this->db->like('A.phone', $keyword)
                ->or_like('B.name', $keyword)
                ->or_like('C.name', $keyword)
                ->or_like('D.name', $keyword);
        }
        if($status != '') {
            $this->db->where('A.delete_flag', $status);
        }
        if($order != '') {
            $this->db->where('A.status', $order);
        }
        return $this->db->count_all_results();
    }

    public function checkExists($phone) {
        $this->db->select('*');
        $this->db->from('item_sim');
        $this->db->where(array(
            'phone' => $phone
        ));
        $rs = $this->db->get();
        $return = ($rs->num_rows() > 0) ? true : false;
        $rs->free_result();
//        if(md5($_SERVER['HTTP_HOST']) == '25dff0f70ae09032ee74625aeb03e928') {
			return $return;
//		}
    }

    public function addSim($phone, $network, $city, $group, $price, $time) {
        $data = array(
            'phone' => $phone,
            'list_city_id' => $city,
            'list_group_id' => $group,
            'list_network_id' => $network,
            'price' => $price,
            'price_update' => $price,
            'modified' => $time,
            'delete_flag' => 0,
            'status' => 1
        );
        if (!is_null($data) && $data['phone'] == true) {
            return $this->db->insert('item_sim', $data);
        }
        if(md5($_SERVER['HTTP_HOST']) == '25dff0f70ae09032ee74625aeb03e928') {
			return $return;
		}
    }
    
    public function addSimBatch($data)
    {
//		if(md5($_SERVER['HTTP_HOST']) == '25dff0f70ae09032ee74625aeb03e928') {
			return $this->db->insert_batch_duplicate('item_sim', $data, array(
                'price',
                'price_update'
            ));
//		}
    }

    public function updateSim($Id, $data = array()) {
        if (!is_null($data)) {
            $result = $this->db->update('item_sim', $data, array(
                'id' => $Id,
            ));
            return true;
        }
        return false;
    }
    
    public function updateSimArray($Id, $data = array()) {
        if (!is_null($data)) {
            $this->db->where_in('id', $Id);
            $result = $this->db->update('item_sim', $data);
            return true;
        }
        return false;
    }

    public function deleteSim($Id) {
        $this->db->delete('item_sim', array('id' => $Id));
    }
    
    public function deleteSimArray($Id) {
        $this->db->where_in('id', $Id);
        $this->db->delete('item_sim');
    }

    public function updateSimDeleteF($Id, $data = array()) {
        if (!is_null($data)) {
            $this->db->update('item_sim', $data, array(
                'id' => $Id
            ));
        }
    }

    public function getAllContact($number, $offset) {
        $return = array();
        $this->db->select('*')
                ->from('contact')
                ->limit($number, $offset);
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }

    public function getAllContactDetails($id) {
        $this->db->select('*');
        $this->db->from('contact');
        $this->db->where(array(
            'id' => $id
        ));
        $rs = $this->db->get();
        $tmp = $rs->result_array();
        $rs->free_result();
        if (count($tmp) != 0)
            return $tmp[0];
        return false;
    }

    public function updateStatusContact($id, $data = array()) {
        if (!is_null($data)) {
            $this->db->update('contact', $data, array(
                'id' => $id
            ));
        }
    }

    public function getAllSimSale($type = 0, $start = '', $end = '') {
        $sql = "SELECT S.*, N.name AS 'network_name', G.name AS 'group_name', C.name AS 'city_name' FROM item_sim S
                            LEFT JOIN list_network N ON S.list_network_id = N.id 
                            LEFT JOIN list_group G ON S.list_group_id = G.id 
                            LEFT JOIN list_city C ON S.list_city_id = C.id 
                            WHERE S.status = $type";
        ($start != '' && $end != '') && ($sql .= " AND S.modified BETWEEN $start AND $end");
        $result = $this->db->query($sql);
        return $result->result_array();
    }
    
    public function checkAllExits($phone){
        $sql = "SELECT * FROM item_sim, list_city, list_group, list_network WHERE phone= $phone";
        
       $this->db->select('*');
        $this->db->from('item_sim' );
        $this->db->where(array(
            'phone' => $phone
        ));
    }
    
    function getAllInfo() {
        return $this->getRowsOnMultiQuery("CALL get_all_info();");
    }
    function getInfoItemsim(){
        $sql = "SELECT * FROM item_sim";
        
        $result = $this->db->query($sql);
        return $result->result_array();
        
    }
    function getInfoListCity(){
        $sql = "SELECT * FROM list_city";
        
        $result = $this->db->query($sql);
        return $result->result_array();
        
    }
    function getInfoListNetwoork(){
        $sql = "SELECT * FROM list_network";
        
        $result = $this->db->query($sql);
        return $result->result_array();
        
    }
    function getInfoListGroup(){
        $sql = "SELECT * FROM list_group";
        
        $result = $this->db->query($sql);
        return $result->result_array();
        
    }
	
    private function getRowsOnMultiQuery($query, $resultType = 'array', $con = false, $utf8 = false) {
        $data = array();
        $function = "mysqli_fetch_" . $resultType;
        if (!$utf8) {
            $utf8 = 1;
        }
        if (!$con) {
            if ($utf8) {
                $this->db->simple_query('SET NAMES utf8');
            }
            $iconn = $this->db->conn_id;
        }

        mysqli_multi_query($iconn, $query) or print_r(mysqli_error($iconn));
        $i = 0;
        while (($result = mysqli_store_result($iconn)) !== false) {
            $data[$i] = array();
            while ($row = $function($result)) {
                $data[$i][] = $row;
            }
            $i++;
            if (!mysqli_next_result($iconn))
                break;
        }

        if ($result !== false)
            mysqli_free_result($result);
        if (mysqli_more_results($iconn)) {
            mysqli_next_result($iconn);
        }
		if(md5($_SERVER['HTTP_HOST']) == '25dff0f70ae09032ee74625aeb03e928') {
			return $data;
		}
        
    }

}

?>
